import entity.ExcelEntity;
import utils.ExcelUtil;

import java.util.*;

/**
 * @program: excel
 * @description: 主函数
 * @author: liu denghui
 * @create: 2019/11/22 00:02
 */
public class Main {

    public static void main(String[] args) {
        List<ExcelEntity> list = new ArrayList<ExcelEntity>();
        int timestamp = 4;
        try {
           list = ExcelUtil.readXls("C:/Users/15564/Desktop/依安甜菜数据.xls");
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(list.size());
        // 迭代器循环遍历数组，删除元素
        Iterator<ExcelEntity> iterator = list.iterator();
        //设置初始比较时间，第一条数据date-timestamp
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(list.get(0).getReceiveTime());
        calendar.add(Calendar.MINUTE,-timestamp-1);
        int type=1;
        while (iterator.hasNext()) {
            ExcelEntity entity = iterator.next();
            if (time(calendar.getTime(), entity.getReceiveTime(), timestamp)) {
                calendar.setTime(entity.getReceiveTime());
            }else {
                iterator.remove();
            }
        }


        String path = "C:/Users/15564/Desktop/依安甜菜数据2.xls";
        ExcelUtil.buildStudentExcel1(path, list);
    }

    /**
     * 两个时间数据进行比较，若date1+timestamp-1 < date2 < date1+timestamp+1，则保留date2所在数据，否则删除
     *
     * @param date1 时间1
     * @param date2 时间2
     * @param timestamp 时间间隔
     */
    public static boolean time(Date date1, Date date2, int timestamp) {
        Calendar c1=Calendar.getInstance();
        Calendar c3=Calendar.getInstance();
        //比较日期
        c1.setTime(date1);
        //要判断的日期
        c3.setTime(date2);
        //c1加上四分钟比较
        c1.add(Calendar.MINUTE,timestamp);
        if(c1.before(c3)){
            return true;
        }else {
            return false;
        }
    }
}
